package csc.fresher.finalgroupfour.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.fresher.finalgroupfour.domain.Role;
import csc.fresher.finalgroupfour.domain.User;

@Repository("userDAO")
public class UserDAO {
	@PersistenceContext
	private EntityManager em; // Entity Manager is acquired by container.

	/**
	 * Add a new user.
	 * 
	 * @param user
	 */
	@Transactional
	public void addUser(User user) {
		em.persist(user);
	}

	/**
	 * List all users in DB.
	 * 
	 * @return
	 */
	@Transactional
	public List<User> listUsers() {
		List<User> users = new ArrayList<User>();
		TypedQuery<User> query = em.createQuery("SELECT a FROM User a",
				User.class);

		users = query.getResultList();
		if (users.size() > 0) {
			return users;
		}
		return null;
	}

	/**
	 * Update a user's information
	 * 
	 * @param user
	 */
	@Transactional
	public void updateUser(User user) {
		em.merge(user);
	}

	/**
	 * Remove a user from database.
	 * 
	 * @param user
	 */
	@Transactional
	public void deleteUser(User user) {
		em.remove(user);
	}

	/**
	 * Find weather a user existed or not.
	 * 
	 * @param user
	 * @return
	 */
	@Transactional
	public boolean findUser(User user) {
		return em.contains(user);
	}

	@Transactional
	public boolean checkLogin(String loginId, String password) {

		// -----------Begin transaction-----------

		boolean check = false;
		try {
			// Get a list of accounts from DB

			TypedQuery<User> query = em.createQuery("SELECT c FROM "
					+ User.class.getName()
					+ " c Where c.loginId= :loginId and c.password= :password",
					User.class);

			query.setParameter("loginId", loginId);
			query.setParameter("password", password);

			// check = query.getResultList().size() > 0;
			if (query.getResultList().size() > 0) {
				check = true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		// -----------End transaction-----------
		return check;

	}

	@Transactional
	public User findUser(String loginId) {
		User user = null;
		try {
			// Get a list of accounts from DB

			TypedQuery<User> query = em.createQuery("SELECT c FROM "
					+ User.class.getName() + " c Where c.loginId= :loginId",
					User.class);

			query.setParameter("loginId", loginId);

			// check = query.getResultList().size() > 0;
			user = query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	public List<GrantedAuthority> getAuthorities(User user) {
		// TODO Auto-generated method stub
		/*
		List<GrantedAuthority> authoritiesList = null;
		String loginId = user.getLoginId();
		TypedQuery<User> query = em
				.createQuery("select u.loginId, r.role from"
						+ User.class.getName() + " u," + Role.class.getName()
						+ "r where u.role = r.roleId and u.loginId =:loginId");
		query.setParameter("loginId", loginId);*/
		
		return null;
	}
}
